$(function() {
	$('#main .phone').on('click', function() {
		UM.actionsheet({
			title : '选择照片',
			items : ['从图库中选择', '照相'],
			callbacks : [
			function() {
				//alert('图库');
				summer.openPhotoAlbum({
					compressionRatio : 0.5,
					callback : function(args) {
						$("#returnpicture").attr("src", args.imgPath); 
					}
				});
			},
			function() {
				//alert('照相');
				summer.openCamera({
					compressionRatio : 0.5,
					callback : function(args) {
						$("#returnpicture").attr("src", args.imgPath); 
					}
				});
			}]
		});
	})
	$('#submit').on('click', function() {
		UM.toast({
		    title: '友情提示：',
		    text: '发布成功',
		    duration: 1000
		});
	})
});
$(function() {
	//Knockout绑定
	var RowModel = function(obj) {
		var self = this;
		if ( typeof obj !== "object")
			obj = {};
		self.head = ko.observable(obj.head || "");
		self.nickname = ko.observable(obj.nickname || "");
		self.ifVip = ko.observable(obj.ifVip || false);
		self.source = ko.observable(obj.source || "");
		self.pic = ko.observable(obj.pic || "0");
		self.talk = ko.observable(obj.talk || "￥");
		self.agreeNum = ko.observable(obj.agreeNum || 0);
		self.disagreeNum = ko.observable(obj.disagreeNum || 0);
		self.comments = ko.observable(obj.comments || 0);
		self.hasDeclare = ko.observable("");
	}
	var ViewModel = function(jsonArray) {
		var self = this;
		var data = [];
		jsonArray.forEach(function(item) {
			data.push(new RowModel(item));
		});
		self.data = ko.observableArray(data);
		self.agree = function(item) {
			var agreeNum = item.agreeNum();
			var disagreeNum = item.disagreeNum();
			var declare = item.hasDeclare();
			if (declare == "") {
				item.agreeNum(agreeNum + 1);
			} else if (declare == "disagree") {
				item.agreeNum(agreeNum + 1);
				item.disagreeNum(disagreeNum - 1);
			}
			item.hasDeclare("agree");
		};
		self.disagree = function(item) {
			var agreeNum = item.agreeNum();
			var disagreeNum = item.disagreeNum();
			var declare = item.hasDeclare();
			if (declare == "") {
				item.disagreeNum(disagreeNum + 1);
			} else if (declare == "agree") {
				item.disagreeNum(disagreeNum + 1);
				item.agreeNum(agreeNum - 1);
			}
			item.hasDeclare("disagree");
		}
		self.data = ko.observableArray(data);
	};
	var jsonArray = [{
		"head" : "img/head1.jpg",
		"nickname" : "june123",
		"ifVip" : true,
		"source" : "新浪",
		"pic" : "img/adpic1.png",
		"talk" : "收到了录音笔，很喜欢，作为播放器也是必须点赞",
		"agreeNum" : 12,
		"disagreeNum" : 30,
		"comments" : 50
	}, {
		"head" : "img/head2.jpg",
		"nickname" : "happyday",
		"ifVip" : false,
		"source" : "QQ",
		"pic" : "img/adpic2.png",
		"talk" : "好温馨的感觉！",
		"agreeNum" : 20,
		"disagreeNum" : 10,
		"comments" : 60
	}, {
		"head" : "img/head1.jpg",
		"nickname" : "甜蜜的梦",
		"ifVip" : true,
		"source" : "网易",
		"pic" : "img/adpic3.png",
		"talk" : "可爱的悟空，一直都在我们身边",
		"agreeNum" : 20,
		"disagreeNum" : 35,
		"comments" : 40
	}];
	var viewModel = new ViewModel(jsonArray);
	ko.applyBindings(viewModel);
	//构造控件实例
	var listview = UM.listview("#listview");
	//添加控件方法
	listview.on("pullDown", function(sender) {
		//这是可以编写列表下拉加载逻辑，参数sender即为当前列表实例对象
		/* var row = {
		 "head": "img/head2.jpg",
		 "nickname": "来自星星的你",
		 "ifVip": false,
		 "source": "开心网",
		 "pic": "img/adpic4.png",
		 "talk": "球拍刚拿到手，比想象的好，物美价廉，五星级推荐！！！",
		 "agreeNum": 60,
		 "disagreeNum": 55,
		 "comments": 40
		 };
		 viewModel.data.unshift(new RowModel(row)); */
		sender.refresh();
	});
	listview.on("pullUp", function(sender) {
		//这是可以编写列表上拉刷新逻辑，参数sender即为当前列表实例对象
		/* var row = {
		 "head": "img/head2.jpg",
		 "nickname": "北极星的眼泪",
		 "ifVip": true,
		 "source": "网易",
		 "pic": "img/adpic4.png",
		 "talk": "球拍刚拿到手，比想象的好，物美价廉，五星级推荐！！！",
		 "agreeNum": 60,
		 "disagreeNum": 55,
		 "comments": 40
		 };
		 viewModel.data.push(new RowModel(row)); */
		sender.refresh();
	});
	listview.on("itemClick", function(sender, args) {
		//这里可以处理行点击事件，参数sender即为当前列表实例对象，args对象有2个属性，即rowIndex(行索引)和$target(目标行的jquery对象)
		//alert("您点击了列表第"+ (args.rowIndex+1) + "行！");
	});
});

